package logic

import (
	"context"

	"cloud-disk/core/internal/svc"
	"cloud-disk/core/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type FileUploadPrepareLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewFileUploadPrepareLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FileUploadPrepareLogic {
	return &FileUploadPrepareLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *FileUploadPrepareLogic) FileUploadPrepare(req *types.FileUploadPrepareRequest) (resp *types.FileUploadPrepareResponse, err error) {
	resp = new(types.FileUploadPrepareResponse)
	rp, has, err := l.svcCtx.RepositoryPoolModel.GetRepositoryPoolByHash(req.Md5)
	if err != nil {
		resp.Code = 100
		resp.Message = "获取失败"
		return resp, nil
	}

	if has {
		resp.Code = 200
		resp.Message = "获取成功"
		resp.Identity = rp.Identity
		return resp, nil
	}
	resp.Code = 200
	resp.Message = "获取成功"
	return resp, nil
}
